home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 103_01 / edit.c < prev    next >
Text File  |  1985-03-09  |  21KB  |  1,033 lines

  1.  
  2. /*    Hello!  This program is supposed to be an editor...  in fact, the
  3.     editor just like mom used to make (oops...) I mean, just like the
  4.     QED editor found most anywhere, especially on UNIX
  5. */
  6.  
  7. #include "edit.inc"
  8.  
  9. main(argc,argv)
  10.     int argc;
  11.     char **argv;
  12.     {
  13.     char *exp_string;
  14.     int i;
  15.     init_buffer();
  16.     prompt_enabled = DEF_PROMPT;
  17.     sprintf(deflt_file,"DEFAULT.$$$");
  18.     print_line_number = DEFAULT_PRINT_LINE_NUMBER;
  19.     exp_string = "+-0123456789.$/?";
  20.     modified_file = NO;
  21.     printer_echo = NO;
  22.     push_point = 0;
  23.     user_command = NO;
  24.     user_string[0] = EOL;
  25.     printf("version 12.9999b by scott fluhrer and neal somos");
  26.     putchar('\n');    /* to force local version in */
  27.     if (argc>1)
  28.         if (argc==2)
  29.             {
  30.             read_file(argv[1]);
  31.             }
  32.         else
  33.             syntax_error();
  34.     for (;;)
  35.         {
  36.         if (user_command)
  37.             {
  38.             for (i=0; line[i] = user_string[i]; i++)
  39.                 ;
  40.             user_command = NO;
  41.             }
  42.         else
  43.             {
  44.             tline[0]='\0';
  45.             if (prompt_enabled)
  46.                 {
  47.                 if (print_line_number)
  48.                     sprintf(tline,"%d>",dot);
  49.                 else    sprintf(tline,">");
  50.                 }
  51.             get_line(line,tline);
  52.             }
  53.         parse = 0;
  54.         skip_space(&parse, line);
  55.         if (char_in_string(line[parse], exp_string))
  56.             {
  57.             number1 = eval_exp(&parse, line);
  58.             if (number1 == ERROR)
  59.                 SYNTAX_ERROR;
  60.             skip_space(&parse, line);
  61.             if (line[parse] == COMMA)
  62.                 {
  63.                 parse++;
  64.                 number2 = eval_exp(&parse, line);
  65.                 if (number2 == ERROR)
  66.                     SYNTAX_ERROR;
  67.                 num_num = 2;
  68.                 }
  69.             else
  70.                 num_num = 1;
  71.             }
  72.         else
  73.             num_num = 0;
  74.         skip_space(&parse, line);
  75.         global = NO;
  76.         found_line = NO;
  77.         if (do_command())
  78.             continue;
  79. oops:        syntax_error();
  80.         }
  81.     }
  82.  
  83. /*    This is the general command do-er, returning YES if everything
  84.     went Hunky-Dory
  85. */
  86. BOOL do_command()
  87.     {
  88.     int l, start_search, i;
  89.     BOOL interact, forced, delete_after_store, print_control;
  90.     char file[MAX_LINE], replace[MAX_LINE+2], *h;
  91.     char *w;
  92.     char string1[MAX_LINE+1], string2[MAX_LINE+1];
  93.     switch( tolower( line[parse] ))
  94.         {
  95.         case 'p':
  96.             print_control = NO;
  97. random_label:        if (check_2_param())
  98.                 return(NO);
  99.             while(l=get_next_line())
  100.                 {
  101.                 print(l, print_control);
  102.                 dot = l;
  103.                 }
  104.             break;
  105.         case 'l':
  106.             print_control = YES;
  107.             goto random_label;
  108.         case EOL:
  109.             if (check_blank())
  110.                 return(NO);
  111.             while (l=get_next_line())
  112.                 {
  113.                 print(l, NO);
  114.                 dot = l;
  115.                 }
  116.             break;
  117.         case 'i':
  118.             if (check_1_param())
  119.                 return(NO);
  120.             if (number1 == 0)
  121.                 return(NO);
  122.             insert(number1);
  123.             break;
  124.         case 'a':
  125.             if (tolower( line[parse+1] ) == 'c')
  126.                 {
  127.                 parse++;
  128.                 if (check_2_param())
  129.                     return(NO);
  130.                 while (l=get_next_line())
  131.                     append_comment(l);
  132.                 break;
  133.                 }
  134.             if (check_1_param())
  135.                 return(NO);
  136.             if (number1 > dollar)
  137.                 return(NO);
  138.             insert(number1+1);
  139.             break;
  140.         case 'd':
  141.             if (check_2_param())
  142.                 return(NO);
  143.             while(l=get_next_line())
  144.                 delete(l);
  145.             break;
  146.         case 'c':
  147.             if (check_2_param())
  148.                 return(NO);
  149.             if (global)
  150.                 return(NO);
  151.             while (l=get_next_line())
  152.                 delete(l);
  153.             insert(number1);
  154.             break;
  155.         case 'm':
  156.             delete_after_store = YES;
  157. move_label:        if (check_3_param())
  158.                 return(NO);
  159.             stuffed_file = NO;
  160.             t = temp_buff;
  161.             while (l=get_next_line())
  162.                 {
  163.                 read_line(l, replace);
  164.                 if (delete_after_store)
  165.                     delete_line(l);
  166.                 for (w=replace; *w; w++)
  167.                     if (save_in_buff(*w))
  168.                         return (NO);
  169.                 if (save_in_buff(EOL))
  170.                     return (NO);
  171.                 }
  172.             if (stuffed_file)
  173.                 seek(fd, 0, 0);
  174.             u = 0;
  175.             while (recall_from_buff(replace))
  176.                 {
  177.                 insert_line(number3, replace);
  178.                 }
  179.             break;
  180.         case 't':
  181.             delete_after_store = NO;
  182.             goto move_label;
  183.         case 'h':
  184.             if (check_0_param())
  185.                 return(NO);
  186.             if ((fd=open("edit.hlp", 0)) == ERROR)
  187.                 return(NO);
  188.             read(fd, temp_buff, 8);
  189.             close(fd);
  190.             for (h=&temp_buff; *h!=CNTRL_Z; h++)
  191.                 printf("%c",*h);
  192.             break;
  193.         case 'u':
  194.             if (line[parse+1] == '=')
  195.                 {
  196.                 if (check_user())
  197.                     return(NO);
  198.                 parse += 2;
  199.                 if (line[parse] == EOL)
  200.                     {
  201.                     printf("%s\n", user_string);
  202.                     break;
  203.                     }
  204.                 for (i=0; line[parse];)
  205.                     user_string[i++] = line[parse++];
  206.                 user_string[i] = EOL;
  207.                 break;
  208.                 }
  209.             if (check_0_param())
  210.                 return(NO);
  211.             user_command = YES;
  212.             break;
  213.         case 'j':
  214.             if (check_1_param())
  215.                 return(NO);
  216.             if (number1==0 OR number1==dollar)
  217.                 return(NO);
  218.             read_line(number1, string1);
  219.             read_line(number1+1, string2);
  220.             if (strlen(string1) + strlen(string2) > MAX_LINE)
  221.                 {
  222.                 printf("Line too long\007\n");
  223.                 return(YES);
  224.                 }
  225.             strcat(string1, string2);
  226.             delete_line(number1+1);
  227.             delete_line(number1);
  228.             insert_line(number1+1, string1);
  229.             dot = number1+1;
  230.             break;
  231.         case 'n':
  232.             if (check_file(file))
  233.                 return(NO);
  234.             if (push_point == MAX_FILES)
  235.                 return(NO);
  236.             if (fopen(file, &iobuf[push_point]) == ERROR)
  237.                 return(NO);
  238.             push_point++;
  239.             break;
  240.         case 'r':
  241.             if (check_file(file))
  242.                 return(NO);
  243.             read_file(file);
  244.             break;
  245.         case 'w':
  246.             if (check_file(file))
  247.                 return(NO);
  248.             write_file(file);
  249.             break;
  250.         case 'f':
  251.             if (line[parse+1] == '=')
  252.                 {
  253.                 parse++;
  254.                 if (check_file(file))
  255.                     return(NO);
  256.                 strcpy(deflt_file, file);
  257.                 break;
  258.                 }
  259.             if (strcmp(deflt_file, "") == 0)
  260.                 printf("\007??\n");
  261.             else
  262.                 printf("%s\n", deflt_file);
  263.             break;
  264.         case '#':
  265.             if (check_0_param())
  266.                 return(NO);
  267.             print_line_number ^= YES;
  268.             break;
  269.         case '>':
  270.             if (check_0_param())
  271.                 return(NO);
  272.             prompt_enabled = NO;
  273.             break;
  274.         case '<':
  275.             if (check_0_param())
  276.                 return(NO);
  277.             prompt_enabled = YES;
  278.             break;
  279.         case 'q':
  280.             if (line[parse+1] == '!')
  281.                 {
  282.                 parse++;
  283.                 forced = YES;
  284.                 }
  285.             else
  286.                 forced = NO;
  287.             if (check_0_param())
  288.                 return(NO);
  289.             if (modified_file AND NOT forced)
  290.                 {
  291.                 printf("Write??\007\n");
  292.                 modified_file = NO;
  293.                 break;
  294.                 }
  295.             else
  296.                 {
  297.                 if (temp_used)
  298.                     unlink(TEMP_FILE);
  299.                 exit(YES);
  300.                 }
  301.         case 's':
  302.             interact = NO;
  303. search_label:        if (check_search())
  304.                 return(NO);
  305.             if (NOT global)
  306.                 init_search();
  307.             start_search = place_search(&parse, line);
  308.             if (start_search == ERROR)
  309.                 return(NO);
  310.             if (NOT get_replace(&parse, line, replace))
  311.                 return(NO);
  312.             g_mode = p_mode = NO;
  313.             if (NOT get_options())
  314.                 return(NO);
  315.             if (interact)
  316.                 {
  317.                 if (g_mode)
  318.                     return(NO);
  319.                 g_mode = YES;
  320.                 }
  321.             while (l=get_next_line())
  322.                 substitute(l, start_search, replace, interact);
  323.             break;
  324.         case 'x':
  325.             interact = YES;
  326.             goto search_label;
  327.         case 'g':
  328.             search_true = YES;
  329. global_search:        if (check_global())
  330.                 return(NO);
  331.             global = YES;
  332.             init_search();
  333.             if (place_search(&parse, line) == ERROR)
  334.                 return(NO);
  335.             return(do_command());
  336.         case 'v':
  337.             search_true = NO;
  338.             goto global_search;
  339.         case '=':
  340.             if (check_1_param())
  341.                 return(NO);
  342.             printf("%d\n", number1);
  343.             break;
  344.         case '%':
  345.             if (check_1_param())
  346.                 return(NO);
  347.             if (dollar == 0 OR number1 == 0 OR number1>dollar)
  348.                 return(NO);
  349.             dot = number1;
  350.             for (i=max(1, dot-8); i<dot; i++)
  351.                 print(i, NO);
  352.             HIGH_LIGHT;
  353.             print(dot, NO);
  354.             STOP_HIGH_LIGHT;
  355.             for (i=dot+1;i<=min(dot+8, dollar); i++)
  356.                 print(i, NO);
  357.             break;
  358.         case ':':
  359.             if (check_1_param())
  360.                 return(NO);
  361.             if (number1 > dollar OR number1 == 0)
  362.                 return(NO);
  363.             dot = number1;
  364.             for (i=dot; i<=min(dot+16, dollar); i++)
  365.                 print(i, NO);
  366.             break;
  367.         case '^':
  368.             if (check_1_param())
  369.                 return(NO);
  370.             if (dot == 0)
  371.                 return(NO);
  372.             for (;;)
  373.                 {
  374.                 if (kbhit())
  375.                     {
  376.                     getchar();
  377.                     printf("!\007    %d\n", dot);
  378.                     break;
  379.                     }
  380.                 print(dot, NO);
  381.                 if (dot == dollar)
  382.                     break;
  383.                 dot++;
  384.                 }
  385.             break;
  386.         case '&':
  387.             if (check_1_param())
  388.                 return(NO);
  389.             if (number1 > dollar)
  390.                 return(NO);
  391.             dot = number1;
  392.             for (i=max(dot-16, 1); i<=dot; i++)
  393.                 print(i, NO);
  394.             break;
  395.         case QUOTE:
  396.             if (check_1_param())
  397.                 return(NO);
  398.             if (number1>dollar)
  399.                 return(NO);
  400.             for (i=min(number1+